#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>

int main () {
        int i, n, k[10] = {adventurer, council_room, feast, gardens, mine, remodel, smithy, village, baron, salvager};
        int result;
        int cardDrawn;
        struct gameState G;

        printf("----Random testing of Adventurer Card----\n");
        printf("Starting Random tests\n");
        int numOfTests = floor(Random() * 10000);

        for(n = 0; n < numOfTests ; n++)
        {
           for (i = 0; i < sizeof(struct gameState); i++) {
            ((char*)&G)[i] = floor(Random() * 256);
        }
                //Initialize the game with random players
                result = initializeGame((rand() % 3 + 2), k, rand(), &G);
                assert(result == 0);

   	     	int player = 0;
        	int treasureCountBeforePlay =0;

                 G.discardCount[player] = 30;
                 G.deckCount[player] = 0; 
	        for(i = 0; i < G.discardCount[player]; i++){
                     cardDrawn = G.discard[player][i];
                     if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
     	                  treasureCountBeforePlay++;
     		 }

      for(i = 0; i < G.deckCount[player]; i++){
                     cardDrawn = G.deck[player][i];
                     if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
                          treasureCountBeforePlay++;
        	}

                //Count the treasures in hand before playing the Adventurer action card
                int numTreasureHandPre = 0;
                for (i = 0; i < G.handCount[player]; i++) {
  //              printf ("Hand count before Adventurer play %d, ", G.handCount[player]);
   //             printf ("\n");
                        cardDrawn = G.hand[player][i];
                        if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
                                numTreasureHandPre++;
                }

                //Play the Adventurer action card
                result = cardEffect(adventurer, 0, 0, 0, &G, 0, 0);
                assert(result == 0);

                //Count treasures in hand after playing card
                int numTreasureHandPost = 0;
                for (i = 0; i < G.handCount[player]; i++) {
//                     printf ("Hand count after Adventurer play %d, ", G.handCount[player]);
 //                    printf ("\n");
                         cardDrawn = G.hand[player][i];
                        if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
                                numTreasureHandPost++;
                }

//                 int prec = numTreasureHandPre +2;
 //                               printf ("numTreasureHandPre is %d, ", prec);
  //                              printf ("\n");
   //                             printf ("numTreasureHandPost is %d, ", numTreasureHandPost);
    //                            printf ("\n");

	        int treasureCountAfterPlay =0;
       	        for(i = 0; i < G.discardCount[player]; i++){
                    cardDrawn = G.discard[player][i];
                      if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
                          treasureCountAfterPlay++;
                 }
	        for(i = 0; i < G.deckCount[player]; i++){
       	            cardDrawn = G.deck[player][i];
          	      if (cardDrawn == copper || cardDrawn == silver || cardDrawn == gold)
               	          treasureCountAfterPlay++;
                 }	
         printf ("Play number %d \n" , n+1);
         printf ("Number of Treasure cards in 'deckCount & DiscardCount' before Adventurer card play is %d \n" , treasureCountBeforePlay);
         printf ("Number of Treasure cards in 'deckCount & DiscardCount' after Adventurer card play (treasureCountBeforePlay -2) is %d  \n" , treasureCountAfterPlay );
         printf ("Number of Treasure cards in hand before play is %d  \n" , numTreasureHandPre);
         printf ("Number of Treasure cards in hand after play ( numTreasureHandPre +2) is %d  \n" , numTreasureHandPost);
         printf ("\n");

                //Ensure that two additional treasures have been added to hand after playing Adventurer card 
      //           assert((numTreasureHandPre + 2) == numTreasureHandPost);
                //Ensure that two treasures cards have been decremented from "deckCount & DiscardCount" as two cards were added to played hand
     //            assert(treasureCountBeforePlay == (treasureCountAfterPlay +2));

        }

        printf("Random tests completed\n\n");
        return 0;
}
